<?php

namespace App\Admin\Controllers;

use App\Admin\Model\OrderCoupon;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class OrderCouponController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(OrderCoupon::class, function (Grid $grid) {
            $grid->filter(function ($filter) {
                // 在这里添加字段过滤器
                // $filter->like('nickname', '昵称');
                $filter->where(function ($query) {

                    $query->whereHas('users', function ($query) {
                        $query->where('mobile', 'like', "%{$this->input}%");
                    });

                }, '手机号');
                // $filter->like('sex', '性别');
                $filter->in('status', '状态')->multipleSelect([1 => '待支付', 2 => '已支付', 3 => '不知道']);
            });
            $grid->id('ID')->sortable();

            $grid->users('账户')->display(function($users){
                if(!empty($users)){
                    // if($users['nickname']){
                    //     return $users['nickname'];
                    // }else{
                        return $users['mobile'];
                    // }
                }else{
                    return '用户表没这个用户';
                }
            });
            
            // $grid->card_id('会员卡id');
            $grid->order_sn('订单号');
            $grid->transaction_id('第三方订单号');
            $grid->total_price('订单价格');
            $grid->pay_type('支付方式')->display(function($pay_type){
                switch ($pay_type) {
                    case 1:
                       return '微信';
                        break;
                    case 2:
                        return '支付宝';
                        break;
                    case 3:
                        return '银联';
                        break;
                    default:
                        return '意料之外的支付方式';
                        break;
                }
            });
            $grid->status('状态')->editable('select', [1 => '待支付', 2 => '已支付',3=>'不知道']);
             $grid->underline_ctype('卡的类型')->display(function($underline_ctype){
                return $underline_ctype['name'];
            });
            $grid->city_name('城市')->display(function ($city_name) {
                if (empty($city_name)) {
                    return '没有城市名称';
                }else{
                    return $city_name['name'];
                }
            });
            $grid->user_name('销售人')->display(function($user_name){
                if(empty($user_name)){
                    return '没人';
                }else{
                    return $user_name['nickname'];
                }
                
            });
            $grid->created_at('生成时间');
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(OrderCoupon::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
